 local CrossServerGuildWarHelper = {}

 
function CrossServerGuildWarHelper.isGuildWarFuncOpen()
    local isLarge7 = app.session:isOpenServerTimeLargeDay(7) -- 开服时间
   
    -- 开服7天 
    if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_CS_GUILD_WAR) then
        return true
    end
    return false
end



function CrossServerGuildWarHelper:convertToGuildIconData(pbData)
	local param = {
		baseIconId = pbData.flagBaseIcon,
		baseIconColor = pbData.flagBaseIconColor,
		additionIconId = pbData.flagAdditionalIcon,
		additionIconColor = pbData.flagAdditionalIconColor
    }
	return param
end


-- 获取 获取参赛资格时间
function CrossServerGuildWarHelper.getGuildWarGetTicketTime()
    local t = GD:getCSGuildWarStrPar("StartTime", "21:40:00")
    local tArr = string.split(t, ":")
    return checknumber(tArr[1] or 0), checknumber(tArr[2] or 0), checknumber(tArr[3] or 0)
end


function CrossServerGuildWarHelper.getGuildWarGetTicketTimeStamp(isNext)
    local hour, min, sec = CrossServerGuildWarHelper.getGuildWarGetTicketTime()
    local t = app:getServerTimeByHMSW(hour, min, sec, nil, isNext)
    return t
end

-- 获取 开始时间
function CrossServerGuildWarHelper.getGuildWarStartTimeStamp(isNext)
    local t = CrossServerGuildWarHelper.getGuildWarGetTicketTimeStamp(isNext)
    -- 这里 只做简单的兼容处理
    local startPrepareTime = tonumber(GD:getCSGuildWarPar("StartPrepareTime", "240"))
    t = t  + startPrepareTime
    return t
end


-- 获取 开始时间 + 第一场 战斗的准备时间
function CrossServerGuildWarHelper.getGuildWarStartTimeStampAddFirstPrepare(isNext)
    local t = CrossServerGuildWarHelper.getGuildWarGetTicketTimeStamp(isNext)
    -- 这里 只做简单的兼容处理
    local startPrepareTime = tonumber(GD:getCSGuildWarPar("StartPrepareTime", "240"))
    local battlePrepareTime = tonumber(GD:getCSGuildWarPar("BattlePrepareTime", "60"))
    t = t  + startPrepareTime + battlePrepareTime
    return t
end

function CrossServerGuildWarHelper.getGuildWarStartTime()
    local t = CrossServerGuildWarHelper.getGuildWarStartTimeStampAddFirstPrepare()
    local d = os.date("*t", t)
    return d.hour, d.min, d.sec
end


function CrossServerGuildWarHelper.getGuildWarMaxEndTimeStamp(isNext)
    local t = CrossServerGuildWarHelper.getGuildWarStartTimeStamp(isNext)
    local endTime = tonumber(GD:getCSGuildWarPar("MaxTime", "3600"))
    return t + endTime
end

function CrossServerGuildWarHelper.isTodayFightBegin()
    local t = CrossServerGuildWarHelper.getGuildWarGetTicketTimeStamp(false)
    local curTime = math.floor(app.session:getServerTime()) or os.time()
    if curTime < t then
        return false
    end
    return true
end


-- 
function CrossServerGuildWarHelper.getGuildWarResetTime()
    local hour = tonumber(GD:getSystemPar("daystartsat", 5))
    return hour, 0, 0
end


function CrossServerGuildWarHelper.getGuildCanNotJoinDesc()
    local round = app:getWeekDayPassDailyUpdate()
    if round >= 6 then
        return L("FinialsQualificationExplain")
    else
        return L("RegularQualificationExplain")
    end
end


function CrossServerGuildWarHelper.getGuildCanNotJoinTitle()
    local round = app:getWeekDayPassDailyUpdate()
    if round < 6 then
        return L("guildHasNoRegularQualification")
    else
        return L("guildHasNoFinialsQualification")
    end
end

-- 获取底部 排名 状态str
function CrossServerGuildWarHelper:getStateStr(rank)
    local round = app:getWeekDayPassDailyUpdate()

    if round >= 1 and round <= 5 then
        if UD.csGuildWarData:isMyGuildCanJoin() then
            return L("regularStage")..L("regularStage_1")
        else
            return L("guildHasNoRegularQualification")
        end
    end

    if round == 6 then
        if rank then
            if rank >=1 and rank <= 4 then
                return L("finialsStage1to4")
            elseif rank >=5 and rank <= 8 then
                return L("finialsStage5to8")
            elseif rank >=9 and rank <= 12 then
                return L("finialsStage9to12")
            elseif rank >=13 and rank <= 16 then
                return L("finialsStage13to16")
            end
        end
        return L("guildHasNoFinialsQualification")
    end

    if round == 7 then
        if rank then
            if rank >=1 and rank <= 2 then
                return L("finialsStage1to2")
            elseif rank >=3 and rank <= 4 then
                return L("finialsStage3to4")
            elseif rank >=5 and rank <= 6 then
                return L("finialsStage5to6")
            elseif rank >=7 and rank <= 8 then
                return L("finialsStage7to8")
            elseif rank >=9 and rank <= 10 then
                return L("finialsStage9to10")
            elseif rank >=11 and rank <= 12 then
                return L("finialsStage11to12")
            elseif rank >=13 and rank <= 14 then
                return L("finialsStage13to14")
            elseif rank >=15 and rank <= 16 then
                return L("finialsStage15to16")
            end
        end
        return L("guildHasNoFinialsQualification")
    end
    return ""
end

-- 根据公会排名获取 奖励
function CrossServerGuildWarHelper.getAwardByRank(rank)
    local gds = GD:getCSGuildWarReward()
    local datas = {}
    for k, v in pairs(gds) do
        local rankMin = tonumber(v.guildRankMin)
        local rankMinMax = tonumber(v.guildRankMax)
        if rank >= rankMin and rank <= rankMinMax then
            local name 
            if tonumber(v.combatRankMin) == 0 then
                name = L("position_1")
            else
                if tonumber(v.combatRankMin) == tonumber(v.combatRankMax) then
                    name = string.format( L("CSGuildWarScoreTitle"), v.combatRankMin)
                else
                    name = string.format( L("CSGuildWarScoreTitle2"), v.combatRankMin, v.combatRankMax)
                end
            end
            table.insert( datas, {name = name, rank = tonumber(v.combatRankMin), awards = UD:parseDropToBagItem(v.reward) or {}})
        end
    end
    table.sort( datas, function(a,b)
        return a.rank < b.rank
    end )
    return datas
end

function CrossServerGuildWarHelper.getGuildsRankAward()
    local gds = GD:getCSGuildWarReward()
    local datas = {}
    local temp = {}
    for k, v in pairs(gds) do
        local rankMin = tonumber(v.guildRankMin)
        local rankMax = tonumber(v.guildRankMax)
        if not temp[rankMin] then
            table.insert(datas, {minRank = rankMin, maxRank = rankMax})
            temp[rankMin] = true
        end
    end
    table.sort( datas, function(a,b)
        return a.minRank < b.minRank
    end )
    return datas
end


-- 获取 公会战
function CrossServerGuildWarHelper.getCSGuildWarPrepareMapId()
    local t = tonumber(GD:getCSGuildWarPar("MapId", "0"))
    return t
end

function CrossServerGuildWarHelper.isCSGuildWarPrepareMap()
    local mapId = MapManager:getMapId()
    local t = tonumber(GD:getCSGuildWarPar("MapId", "0"))

    return t == mapId
end


function CrossServerGuildWarHelper.getBattleWinAward()
    local t = tonumber(GD:getCSGuildWarPar("WinReward", 0))
    if t then
        return UD:parseDropToBagItem(t)
    end
    return {}
end

function CrossServerGuildWarHelper.getBattleLoseAward()
    local t = tonumber(GD:getCSGuildWarPar("LoseReward", 0))
    if t then
        return UD:parseDropToBagItem(t)
    end
    return {}
end


-- 是否可以安全获取 跨服工会战 名额
function CrossServerGuildWarHelper.isSafeGetTicket(rank)
    if rank then
        local maxJoinNum = tonumber(GD:getCSGuildWarPar("MaxMemberNum", 100))
        return rank <= maxJoinNum
    end
end


function CrossServerGuildWarHelper.getTasks()
    local tasksStr = GD:getCSGuildWarStrPar("TaskIds")
    local taskArr = string.split(tasksStr, ";")
    -- local Task = require("app.public.dao.Task")
    local tasks = {}
    for k ,v in pairs(taskArr or {}) do
        -- local t = Task.new()
        -- t:initWithId(tonumber(v))
        table.insert(tasks, tonumber(v))
    end
    return tasks
end


function CrossServerGuildWarHelper.isCurSeasonEnd()
    local curTime = math.floor(app.session:getServerTime() or os.time())
    local ticketTime = app:getServerTimeByHMSW(startH, startM, startS, nil, false) -- 准备开始时间
    if app:getWeekDay() == 7 and curTime >= ticketTime then
        return true
    end
    return false
end

return CrossServerGuildWarHelper